<?php

namespace App\Model;


use Illuminate\Support\Facades\DB;

class RoomRole extends BaseModel
{
    protected  $table = 'room';
    protected  $roleTable = 'room_role';

    CONST NOT_CERTIFIED = 0;//未认证
    CONST CERTIFIED = 1;//已认证
    //商业类型0小屋、1CBD
    CONST LODGE = 0;//小屋
    CONST CBD = 1;//cbd

    /**
     * 获取用户角色为管理员的房间
     * @param $userId
     * @return array
     */
    public function getManageRoomIds($userId)
    {
        $data = DB::connection('api')->table($this->roleTable)
            ->where(['user_id'=>$userId, 'role_type'=>self::CERTIFIED])
            ->pluck('room_id');
        return self::objToArray($data);
    }

    /**
     * 获取用户角色为管理员的房间
     * @param $roomIds
     * @return array
     */
    public function getRoomInfo($roomIds)
    {
        $data = DB::Connection('api')->table($this->table)
            ->where('business_type',self::CBD)
            ->whereIn('room_id', $roomIds)
            ->select(['room_id', 'room_uid', 'name', 'certified'])
            ->get();
        return self::objToArray($data);
    }
    /**
     * 获取用户角色为管理员的房间
     * @param $roomIds
     * @return array
     */
    public function updateRoomRole($roomId)
    {
        DB::Connection('api')->table('room')
            ->where('room_id', $roomId)
            ->update(['room_mode'=>0]);
        $data = DB::Connection('api')->table($this->roleTable)
            ->where('room_id', $roomId)
            ->update(['role_type' => 0]);
        return self::objToArray($data);

    }
    /**
     * 获取用户角色为管理员的房间
     * @param $roomIds
     * @return array
     */
    public function updateRoomRoleType($roomId)
    {
        $user=DB::Connection('api')->table('room')
            ->where('room_id', $roomId)
            ->first(['user_id']);
        $user_id=self::objToArray($user);
        $data = DB::Connection('api')->table($this->roleTable)
            ->where('room_id', $roomId)
            ->where('user_id', $user_id['user_id'])
            ->update(['role_type' => 2]);
        DB::Connection('api')->table('room')
            ->where('room_id', $roomId)
            ->update(['room_mode'=>1]);
        return self::objToArray($data);

    }
}

